package com.roy.eureka.client.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Future;

import org.springframework.stereotype.Service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCollapser;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.roy.eureka.api.dto.User;

@Service
public class HystrixCollapserService {
	/**
	 * 将多个相似的请求聚合到一起，一次请求。
	 * @param id
	 * @return
	 */
	@HystrixCollapser(batchMethod = "getUsers", collapserProperties = {
			@HystrixProperty(name = "timerDelayInMilliseconds", value = "1000")//批次执行时间
	})
	public Future<User> getMember(Integer id) {
		System.out.println("执行单个查询的方法");
		return null;
	}
	/**
	 * 一次批量请求
	 * @param ids
	 * @return
	 */
	@HystrixCommand
	public List<User> getMembers(List<String> ids) {
		List<User> mems = new ArrayList<User>();
		for(String id : ids) {
			System.out.println(id);
			User m = new User();
			m.setUserId(id);
			m.setUserName("angus");
			mems.add(m);
		}
		return mems;
	}
}
